package com.baizhi.cmfzmanager.controller;


import com.baizhi.cmfzmanager.entity.Audio;
import com.baizhi.cmfzmanager.service.AudioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AudioController {

    @Autowired
    private AudioService audioService;

    @RequestMapping("/addAudio")
    public Map addAudio(Audio audio , MultipartFile audioFile , HttpServletRequest request){
        Map map = new HashMap();
        try {
            String imagefileName = audioFile.getOriginalFilename();
            ServletContext servletContext = request.getServletContext();
            String realPath = servletContext.getRealPath("/audio");
            audioFile.transferTo(new File(realPath+"/"+imagefileName));
            audio.setAudioUrl("/audio/"+imagefileName);
            audioService.insert(audio);
            map.put("message","添加成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","添加失败！！！");

        }

        return map;
    }

}
